#ifndef __RN8209_C
#define __RN8209_C

#include "rn8209.h"

//////////// 读数据变量 /////////////////
//// 所读数据的寄存器地址
//unsigned char readAddr  = 0;
//// 所读数据的长度
//uint8_t     rdDatasLen  = 0;
// 检验和变量
uint8_t	  rnCheckTimes	    = 0; // 校验和读取并校对的次数；次数 > 5; 超过一定数据后，表明rn8209设备有错误失效。
uint16_t	rnCheckSUM	      = 0; //检验和:0x00-0x10寄存器需要检验
uint16_t	rnCheckSUMRead	  = 0xEE79; //rn8209中读取到的校验和寄存器;默认值=0xEE79。
uint8_t   rnDeviceID[3]     = {0x00,0x00,0x00};
uint8_t   rnIF              = 0; //记录电表事件

void RN_WriteCommand(unsigned char CmdWord){	
    SPI_bit_reset(SPI_CS_PinId);	
	  //写使能命令 写命令之前一定要先写EA，不然写的命令无效
		SPI_SendRead_Byte(CommandRegAddr);
    #ifdef UsingFreeRTOS
	  vTaskDelay(1);
	  #else
	  delayn50us(1);	
	  #endif    
	  //写命令字 
		SPI_SendRead_Byte(CmdWord);
	  #ifdef UsingFreeRTOS
	  vTaskDelay(1);
	  #else
	  delayn50us(1);	
	  #endif    
    SPI_bit_set(SPI_CS_PinId);
	  #ifdef UsingFreeRTOS
	  vTaskDelay(1);
	  #else
	  delayn50us(2);	
	  #endif    
}

// 向RN8209中的寄存器写入数据：
void RN_WriteDatas(uint8_t Addr,uint8_t *datas,uint8_t datalen){		
	  uint8_t i;	
		// 写命令的最高位=1
	  //taskENTER_CRITICAL();
	  SPI_bit_reset(SPI_CS_PinId);
		SPI_SendRead_Byte(Addr | 0x80);
	  #ifdef UsingFreeRTOS
	  vTaskDelay(1);
	  #else
	  delayn50us(1);	
	  #endif    
		for(i = 0; i < datalen; i++){
			  SPI_SendRead_Byte(datas[i]); 
			  #ifdef UsingFreeRTOS
				vTaskDelay(1);
				#else
				delayn50us(1);	
				#endif    
		}
		SPI_bit_set(SPI_CS_PinId);
		#ifdef UsingFreeRTOS
	  vTaskDelay(1);
	  #else
	  delayn50us(2);	
	  #endif    
}

// 读取RN8209中的数据
void RN_ReadDatas(uint8_t Addr,uint8_t datalen){
	  uint8_t i = 0;	
		// 读数据的地址最高位=0
	  SPI_bit_reset(SPI_CS_PinId);	  
		SPI_SendRead_Byte(Addr & 0x7f);	
	  #ifdef UsingFreeRTOS
	  vTaskDelay(1);
	  #else
	  delayn50us(1);	
	  #endif    
	  // 读取数据
	  for(i = 0; i < datalen; i++){
			  rxDataBufSPI[i] = SPI_SendRead_Byte(0x00); 
			  #ifdef UsingFreeRTOS
				vTaskDelay(1);
				#else
				delayn50us(1);	
				#endif    
		}
	  SPI_bit_set(SPI_CS_PinId);
		#ifdef UsingFreeRTOS
	  vTaskDelay(1);
	  #else
	  delayn50us(2);	
	  #endif    
}

// 初始化校表设置
void RN_Set_Regulation(){
    uint8_t   writeData[2] = {0x00};
    rnCheckSUM = 0;
		// 读取状态寄存器：
    // 写使能
	  RN_WriteCommand(Con_WrEn);
	  // 开始写数据：
		// 关闭通道B：		
	  writeData[0] = 0x00; 
		writeData[1] = 0x00;		
		RN_WriteDatas(SYSCON,writeData,SYSCON_Len);
		rnCheckSUM = rnCheckSUM + 256 * writeData[0] + writeData[1];
		// 过零引脚使能
		writeData[0] = 0x00;//高字节
		writeData[1] = 0x83;
		RN_WriteDatas(EMUCON,writeData,EMUCON_Len);
		rnCheckSUM = rnCheckSUM + 256 * writeData[0] + writeData[1];
		// HFConst设置
		writeData[0] = HFConstValue1;
		writeData[1] = HFConstValue2;
		RN_WriteDatas(HFConst,writeData,HFConst_Len);
		rnCheckSUM = rnCheckSUM + 256 * writeData[0] + writeData[1];
		// 功率增益校正
		writeData[0] = GPQAValue1;
		writeData[1] = GPQAValue2;
		RN_WriteDatas(GPQA,writeData,GPQA_Len);
		rnCheckSUM = rnCheckSUM + 256 * writeData[0] + writeData[1];
		// 电流有效值的offset补偿
		writeData[0] = IARMSOSValue1;
		writeData[1] = IARMSOSValue2;
		RN_WriteDatas(IARMSOS,writeData,IARMSOS_Len);
		rnCheckSUM = rnCheckSUM + 256 * writeData[0] + writeData[1];
		
		// 校验和添加非0的未设置项的默认值：
		rnCheckSUM = rnCheckSUM + PStartValueDef + QStartValueDef;
		// 检验和取反
		rnCheckSUM = ~rnCheckSUM;
		
    // 写保护开启	
	  RN_WriteCommand(Con_WrDis);	  
}



#endif

